package ComplateFuture;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @Author MJS
 * @Date 2021/9/28 12:05
 * @Description
 */
public class CompletableFutureTest {




    public static void main(String[] args) {
        long time6 = System.currentTimeMillis();
        System.out.println("开始时间:"+time6);
        testComFuture();
//        testTime();
        long time3 = System.currentTimeMillis();
        System.out.println("结束时间:"+time3);
    }

    private static void testTime() {
        for (int i = 0; i < 100000000; i++) {
            if(i==99999999){
                long time1 = System.currentTimeMillis();
                System.out.println("线程1:"+time1);
            }
        }
        for (int i = 0; i < 100000000; i++) {
            if(i==99999999){
                long time4 = System.currentTimeMillis();
                System.out.println("线程11:"+time4);
            }
        }

        for (int i = 0; i < 100000000; i++) {
            if(i==99999999){
                long time5 = System.currentTimeMillis();
                System.out.println("线程12:"+time5);
            }
        }

        for (int i = 0; i < 100000000; i++) {
            if(i==99999999){
                long time2 = System.currentTimeMillis();
                System.out.println("线程2:"+time2);
            }
        }

    }

    public static void testComFuture(){

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,
                20,
                4,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(10));
        System.out.println("进入方法时间1："+System.currentTimeMillis());

        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {

            System.out.println("进入方法时间2："+System.currentTimeMillis());
            for (int i = 0; i < 100000000; i++) {
                if(i==99999999){
                    long time1 = System.currentTimeMillis();
                    System.out.println("线程1:"+time1);
                }
            }
            Integer i = 10;
            Integer j = 5;
            Integer o = i / j;
            return o;
        },threadPoolExecutor);


        CompletableFuture<Void> ICompletableFuture = integerCompletableFuture.thenAcceptAsync(o -> {
            for (int i = 0; i < 100000000; i++) {
                if(i==99999999){
                    long time4 = System.currentTimeMillis();
                    System.out.println("线程11:"+time4);
                }
            }

        },threadPoolExecutor);

        CompletableFuture<Void> JCompletableFuture = integerCompletableFuture.thenAcceptAsync(o -> {
            for (int i = 0; i < 100000000; i++) {
                if(i==99999999){
                    long time5 = System.currentTimeMillis();
                    System.out.println("线程12:"+time5);
                }
            }

        },threadPoolExecutor);


        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            for (int i = 0; i < 100000000; i++) {
                if(i==99999999){
                    long time2 = System.currentTimeMillis();
                    System.out.println("线程2:"+time2);
                }
            }
        },threadPoolExecutor);

        CompletableFuture.allOf(ICompletableFuture,
                JCompletableFuture,
                voidCompletableFuture).join();

    }


}
